namespace Nutils
{
    public class Common
    {
        public static void CopyFile(string src_path, string dest_path)
        {
            // Create the temp file from _prebundle_file_path
            using (System.IO.FileStream src = System.IO.File.OpenRead(src_path))
            {
                using (System.IO.FileStream dest = System.IO.File.Create(dest_path))
                {
                    byte[] buffer = new byte[1024];
                    int bytesRead = 0;
                    while (0 != (bytesRead = src.Read(buffer, 0, buffer.Length)))
                    {
                        dest.Write(buffer, 0, bytesRead);
                    }
                }
            }
        }

        public static void DiffFiles(string pathA, string pathB)
        {
            using (System.IO.FileStream originalFileStream = System.IO.File.OpenRead(pathA))
            {
                using (System.IO.FileStream extractedFileStream = System.IO.File.OpenRead(pathB))
                {
                    NUnit.Framework.Assert.AreEqual(originalFileStream.Length, extractedFileStream.Length, "Files {2} and {3} are different lengths. Original {0}, Extracted {1}", originalFileStream.Length, extractedFileStream.Length, pathA, pathB);

                    // Read in each of the files.
                    byte[] original = new byte[originalFileStream.Length];
                    using (System.IO.BinaryReader originalBinReader = new System.IO.BinaryReader(originalFileStream))
                    {
                        originalBinReader.Read(original, 0, (int)originalFileStream.Length);
                    }

                    byte[] extracted = new byte[extractedFileStream.Length];
                    using (System.IO.BinaryReader extractedBinReader = new System.IO.BinaryReader(extractedFileStream))
                    {
                        extractedBinReader.Read(extracted, 0, (int)extractedFileStream.Length);
                    }

                    // diff them one byte at a time.
                    for (long i = 0; i < original.Length; ++i)
                    {
                        NUnit.Framework.Assert.AreEqual(original[i], extracted[i], "Diffing byte {0} from {1}", i, pathA);
                    }
                }
            }
        }
    }
}